package com.tencent.sr.iris.activity.service.event.handler;

import com.tencent.sr.iris.activity.domain.event.dto.ActivityDTO;
import com.tencent.sr.iris.activity.domain.event.dto.ActivityTaskDTO;
import com.tencent.sr.iris.activity.interfaces.event.dto.ActivityEventDTO;
import com.tencent.sr.iris.activity.service.activity.IrisActivityService;
import com.tencent.sr.iris.activity.service.event.activity.IrisActivityExecutor;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Component;

/**
 * 注册有礼
 *
 * @author feiyyin
 * @date 2023/2/17
 */
@Slf4j
@Component("BECOME_TRAINEE")
public class UserBecomeTraineeHandler extends AbstractIrisEventHandler {

    @Resource
    private IrisActivityService activityService;

    @Override
    public void after(ActivityEventDTO eventDTO) {

    }

    @Override
    void process(ActivityEventDTO eventDTO, List<String> eventHandingInfoList) {
        //补充pid
        addParameterForEvent(eventDTO);
        List<ActivityDTO> activity = getActivity(eventDTO);
        activity.forEach(a -> {
            IrisActivityExecutor activityExecutor = getActivityExecutor(eventDTO, a);
            activityExecutor.execute(eventDTO, a);
            eventHandingInfoList
                    .add(this.getClass().getSimpleName() + "处理成功,idempotent:" + eventDTO.getIdempotent());
        });
    }


    @Override
    List<ActivityTaskDTO> getRelatedTaskList(String eventType) {
        return null;
    }

    @Override
    void baseDataProcess(ActivityEventDTO eventDTO) {

    }

    protected List<ActivityDTO> getActivity(ActivityEventDTO msg) {
        List<ActivityDTO> activityDTOS = Lists.newArrayList();
        // 查询活动信息
        ActivityDTO activity = activityService.getActivity(activityConfig.getRegistGiftActivityId());
        if (Objects.isNull(activity)) {
            log.error("未查询到注册有礼活动信息,activityBizId={}", activityConfig.getLevelUpFosterActivityId());
        } else {
            activityDTOS.add(activity);
        }

        // 查询邀请有礼活动
        ActivityDTO inviteActivity = activityService.getActivityAllTask(activityConfig.getInviteGiftActivityId());
        if (Objects.isNull(inviteActivity)) {
            log.error("未查询到邀请有礼活动信息,activityBizId={}", activityConfig.getInviteGiftActivityId());
        } else {
            activityDTOS.add(inviteActivity);
        }

        // 查询月月领活动
        ActivityDTO activityDTO = activityService.getActivity(activityConfig.getMonthGiftActivityId());
        if (Objects.isNull(activityDTO)) {
            log.error("未查询到月月领活动信息,activityBizId={}", activityConfig.getMonthGiftActivityId());
        } else {
            activityDTOS.add(activityDTO);
        }

        return activityDTOS;
    }
}
